﻿Shader "Hidden/Universal Render Pipeline/GlitchImageBlock"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    
    HLSLINCLUDE

    #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"

    TEXTURE2D(_MainTex);		SAMPLER(sampler_MainTex);
    uniform half3 _Params;
    uniform half4 _Params2;
    uniform half3 _Params3;

    #define _TimeX _Params.x
    #define _Offset _Params.y
    #define _Fade _Params.z

    #define _BlockLayer1U _Params2.x
    #define _BlockLayer1V _Params2.y
    #define _BlockLayer2U _Params2.z
    #define _BlockLayer2V _Params2.w

    #define _RGBSplitIndensity      _Params3.x
    #define _BlockLayer1Indensity   _Params3.y
    #define _BlockLayer2Indensity   _Params3.z

    float randomNoise(float2 seed)
    {
        return frac(sin(dot(seed * floor(_TimeX * 30.0), float2(127.1, 311.7))) * 43758.5453123);
    }

    float randomNoise(float seed)
    {
        return randomNoise(float2(seed, 1.0));
    }

    float4 FragImageBlock(Varyings i) : SV_Target
    {
        float2 uv = i.texcoord.xy;
		
		float2 blockLayer1 = floor(uv * float2(_BlockLayer1U, _BlockLayer1V));
		float2 blockLayer2 = floor(uv * float2(_BlockLayer2U, _BlockLayer2V));
		
		float lineNoise1 = pow(randomNoise(blockLayer1), _BlockLayer1Indensity);
		float lineNoise2 = pow(randomNoise(blockLayer2), _BlockLayer2Indensity);
		float RGBSplitNoise = pow(randomNoise(5.1379), 7.1) * _RGBSplitIndensity;
		float lineNoise = lineNoise1 * lineNoise2 * _Offset - RGBSplitNoise;

        float4 colorR = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
        float4 colorG = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(lineNoise * 0.05 * randomNoise(7.0), 0));
        float4 colorB = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - float2(lineNoise * 0.05 * randomNoise(23.0), 0));

        float4 result = float4(float3(colorR.x, colorG.y, colorB.z), colorG.a + colorG.a + colorB.a);
        result = lerp(colorR, result, _Fade);
        return result;
    }

    float4 FragDebug(Varyings i): SV_Target
	{
		float2 uv = i.texcoord.xy;
		
		float2 blockLayer1 = floor(uv * float2(_BlockLayer1U, _BlockLayer1V));
		float2 blockLayer2 = floor(uv * float2(_BlockLayer2U, _BlockLayer2V));
		
		float lineNoise1 = pow(randomNoise(blockLayer1), _BlockLayer1Indensity);
		float lineNoise2 = pow(randomNoise(blockLayer2), _BlockLayer2Indensity);
		float RGBSplitNoise = pow(randomNoise(5.1379), 7.1) * _RGBSplitIndensity;
		float lineNoise = lineNoise1 * lineNoise2 * _Offset - RGBSplitNoise;
		
		return float4(lineNoise, lineNoise, lineNoise, 1);
	}

    struct appdata
    {
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
    };

	 Varyings vert(appdata v)
    {
        Varyings o;
        o.positionCS = TransformObjectToHClip(v.vertex);
        o.texcoord = v.uv;
        return o;
    }
    
    ENDHLSL
    
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment FragImageBlock
            ENDHLSL
        }
    }
}